//
//  LocalStorage.m
//  OCBase
//
//  Created by 侯伟 on 2019/5/5.
//  Copyright © 2019 侯伟. All rights reserved.
//

#import "LocalStorage.h"

@implementation LocalStorage

static UserDefaults* _userDefaults;
static UserDefaults* _shareDefaults;


+(UserDefaults*)share{
    if(!_shareDefaults){
        _shareDefaults = [[UserDefaults alloc] initWithName:UserDefaultSuiteName];
    }
    return _shareDefaults;
}

+(UserDefaults*)user{
    if(_userDefaults){
        return _userDefaults;
    }
    return [self share];
}


+(void)setUserDefaultsWidth:(NSString*)idOrName{
    if(!idOrName.length)
        _userDefaults = NULL;
    
    UserDefaults* userDefaults = [[UserDefaults alloc] initWithName:idOrName];
    _userDefaults = userDefaults;
}

+(void)resetUser{
    _userDefaults = NULL;
}

/**
 保存数据
 
 @param object 待保存的数据
 @param key 待保存的键值
 */
+ (void)setObject:(id)object forKey:(NSString *)key
{
    [LocalStorage.user setObject:object forKey:key];
}

/**
 获取对应键值的数据
 
 @param key 待取数据的键值
 */
+ (id)objectForKey:(NSString *)key
{
    return [LocalStorage.user objectForKey:key];
}

+ (NSInteger)integerForKey:(NSString*)key{
    return [LocalStorage.user integerForKey:key];
}

+ (float)floatForKey:(NSString*)key{
    return [LocalStorage.user floatForKey:key];
}

+ (double)doubleForKey:(NSString*)key{
    return [LocalStorage.user doubleForKey:key];
}

+ (NSString*)stringForKey:(NSString*)key{
    return [LocalStorage.user stringForKey:key];
}

+ (BOOL)boolForKey:(NSString*)key{
    return [LocalStorage.user boolForKey:key];
}

+ (NSString*)stringForKey:(NSString*)key defaultValue:(NSString*)value{
    NSString* obj = [LocalStorage.user stringForKey:key];
    if(obj){
        return obj;
    }else{
        return value;
    }
}

+ (NSArray*)arrayForKey:(NSString*)key{
    return [LocalStorage.user arrayForKey:key];
}

+ (NSDictionary*)dictionaryForKey:(NSString*)key{
    return [LocalStorage.user dictionaryForKey:key];
}

+ (NSData*)dataForKey:(NSString*)key{
    return [LocalStorage.user dataForKey:key];
}

+ (void)removeObjectForKey:(NSString*)key{
    [LocalStorage.user removeObjectForKey:key];
}
@end
